package com.zero.mms.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.neusoft.chenanqi.dto.EmpSalDto;
import com.neusoft.chenanqi.service.FixedSalService;
import com.neusoft.chenanqi.util.ImportExcelUtil;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.Dep;
import com.zero.mms.model.Employee;

@Controller("fixedSalaryController")
@RequestMapping("fixedSalary")
public class FixedSalaryController {
	
	@Inject
	private FixedSalService fixedSalService;

	@RequestMapping("/searchFixedSal")
	@ResponseBody
	public Object searchSal(String depName, String empNo, String startMonth, 
			String endMonth, String salFrom, String salTo) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();

		try {
			Pager<EmpSalDto> empSalDtoPager = fixedSalService.listFixedSal(depName, empNo, startMonth, endMonth, salFrom, salTo);
			objectNode.putPOJO("response", empSalDtoPager);
			objectNode.put("message", "获取固定工资列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/updateFixedSal")
	@ResponseBody
	public Object updateEmpSal(String empNo, String month, 
			@RequestParam(value = "fixedSalName[]") String[] fixedSalName, 
			@RequestParam(value = "fixedSalValue[]") String[] fixedSalValue) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			fixedSalService.updateFixedSal(empNo, month, fixedSalName, fixedSalValue);
			objectNode.put("message", "更新固定工资信息成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/listDepName")
	@ResponseBody
	public Object listDep() {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			List<Dep> depList = fixedSalService.listDep();
			objectNode.putPOJO("response", depList);
			objectNode.put("message", "获取部门列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/listEmpName")
	@ResponseBody
	public Object listEmp(String depId) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			List<Employee> empList = fixedSalService.listEmp(depId);
			objectNode.putPOJO("response", empList);
			objectNode.put("message", "获取员工列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/addEmpFixedSal")
	@ResponseBody
	public Object addEmpFixedSal(String empId, String startMonth, String endMonth, 
			@RequestParam(value = "fixedSalName[]") String[] fixedSalName, 
			@RequestParam(value = "fixedSalValue[]") String[] fixedSalValue) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			fixedSalService.addEmpFixedSal(empId, startMonth, endMonth, fixedSalName, fixedSalValue);
			objectNode.put("message", "添加固定工资信息成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/importFromExcel")
	@ResponseBody
	public Object importFromExcel(String startMonth, String endMonth, 
			@RequestParam(value = "file") CommonsMultipartFile file) {
		
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			List<EmpSalDto> empSalDtoList = ImportExcelUtil.importEmpSal(file.getInputStream());
			fixedSalService.importFixedSal(startMonth, endMonth, empSalDtoList);
			objectNode.put("message", "导入成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
		} catch (NumberFormatException e) {
			objectNode.put("message", "文件中存在不合法字符");
			objectNode.put("error", SmsErrorConstant.INVALID_AUGUMENT);
			objectNode.put("statusCode", SmsStatusCode.INVALID_CODE);
		} catch (ArrayIndexOutOfBoundsException e) {
			objectNode.put("message", "文件格式不正确");
			objectNode.put("error", SmsErrorConstant.INVALID_AUGUMENT);
			objectNode.put("statusCode", SmsStatusCode.INVALID_CODE);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
}
